﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace NettyDeomo.msg
{
    public class MsgManager
    {
        private static bool inited = false;
        private static MsgManager _msgManager = new MsgManager();
        private  Dictionary<int, Type> msgId2Type = new Dictionary<int, Type>();
        private  Dictionary<Type, int> Msgtype2Id = new Dictionary<Type, int>();

        
        public static MsgManager getInstance()
        {
            if (!inited)
            {
                init();
            }

            return _msgManager;
        }

        private static void init()
        {
            _msgManager.registerMsgType();
            inited = true;
        }

        private  void registerMsgType()
        {
            
            msgId2Type.Add(getId<S2C_SyncId>(),typeof(S2C_SyncId));
            Msgtype2Id.Add(typeof(S2C_SyncId),getId<S2C_SyncId>());
            
            msgId2Type.Add(getId<S2C_MonsterMoveTo>(),typeof(S2C_MonsterMoveTo));
            Msgtype2Id.Add(typeof(S2C_MonsterMoveTo),getId<S2C_MonsterMoveTo>());
            
            msgId2Type.Add(getId<C2S_EnterMap>(),typeof(C2S_EnterMap));
            Msgtype2Id.Add(typeof(C2S_EnterMap),getId<C2S_EnterMap>());
            
            msgId2Type.Add(getId<S2C_EnterMap>(),typeof(S2C_EnterMap));
            Msgtype2Id.Add(typeof(S2C_EnterMap),getId<S2C_EnterMap>());
            
            msgId2Type.Add(getId<C2S_EnterMapSuccess>(),typeof(C2S_EnterMapSuccess));
            Msgtype2Id.Add(typeof(C2S_EnterMapSuccess),getId<C2S_EnterMapSuccess>());
            
            msgId2Type.Add(getId<S2C_SynAroundObjs>(),typeof(S2C_SynAroundObjs));
            Msgtype2Id.Add(typeof(S2C_SynAroundObjs),getId<S2C_SynAroundObjs>());

            
            msgId2Type.Add(getId<C2S_Move>(),typeof(C2S_Move));
            Msgtype2Id.Add(typeof(C2S_Move),getId<C2S_Move>());
            
            msgId2Type.Add(getId<S2C_PlayerMove>(),typeof(S2C_PlayerMove));
            Msgtype2Id.Add(typeof(S2C_PlayerMove),getId<S2C_PlayerMove>());
        }

        public Type getMsgType(int msgId)
        {
            if (msgId2Type.ContainsKey(msgId))
            {
                return msgId2Type[msgId];
            }

            return null;
        }



        public  int getId<T>()
        {
            Type type = typeof(T);
            string typeName = type.Name;
            char[] charArray = typeName.ToCharArray();
            int h = 0;
            if (h == 0 && charArray.Length > 0) {
                char[] val = charArray;

                for (int i = 0; i < charArray.Length; i++) {
                    h = 31 * h + val[i];
                }
            }
            return h;
        }
        public  MsgWrapper createMsgWrapper(object msg)
        {

            
            // 获取消息类型
            Type type = msg.GetType();
            if (!_msgManager.Msgtype2Id.ContainsKey(type))
            {
                Debug.LogError("创建消息包装器失败，找不到消息类型："+ type);
                return null;
            }
            MsgWrapper msgWrapper = new MsgWrapper();
            msgWrapper.id = _msgManager.Msgtype2Id[type];
            msgWrapper.msg = msg;
            return msgWrapper;
        }
    }
}